package com.imooc.controller;

import com.imooc.config.MinIOConfig;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.utils.MinIOUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
@Api(value = "文件上传的接口", tags = {"文件上传的controller"})
public class FileController {
    @Autowired
    private MinIOConfig minIOConfig;
    @PostMapping("/upload")
    public GraceJSONResult upload(MultipartFile file) throws Exception {
        String fileNmae = file.getOriginalFilename();
        MinIOUtils.uploadFile(minIOConfig.getBucketName(),fileNmae,file.getInputStream());
        String imgUrl = minIOConfig.getFileHost() + "/" + minIOConfig.getBucketName() + "/" + fileNmae;
        return GraceJSONResult.ok(imgUrl);
    }
}
